<template>
  <div class="detail">
     <van-nav-bar title="面经详情"  left-text="返回" :left-arrow="false" class="header" @click-left="$router.back()">
      <!-- <template #right>
        <van-icon name="search" size="18" />
      </template> -->
     </van-nav-bar>
     <div class="detail-content">
      <h1>{{ obj.stem }}</h1>
     <div class="front ">
        <span>{{ obj.createdAt }}</span>
        <span>|</span>
        <span>{{ obj.views }} 浏览量</span>
        <span>|</span>
        <span> {{ obj.likeCount }} 点赞数</span>
     </div>
     <div class="author">
        <img :src="obj.avatar" alt="" width="40px" height="40px">
        <span>{{ obj.creator }}</span>
     </div>
     <div>
        {{ obj.content }}
     </div>
     </div>
     <div class="function">
         <div class="f1">
          <van-icon name="like-o" size="20px" @click="f1"  />
         </div>
         <div class="f2">
          <van-icon name="star-o" size="20px" @click="f2"   />
         </div>
     </div>
     
  </div>
</template>

<script>
// import router from '@/router';
import axios from 'axios'
export default {
    name:'DetailPage',
    data(){
      return {
        id:'',
        obj:{},
        token:localStorage.getItem('token'),
        f1active:false,
        f2active:false,
      }
    },
    created(){
      this.token = localStorage.getItem('token')
      this.getDetail()
    },
    mounted(){
      console.log(this.$route.params.id)
      console.log(this.token)
    },
    methods:{
      async getDetail(){
        //http://interview-api-t.itheima.net/h5/interview/show  不是管理员端的
        //get 请求 获取单个面经详情
        const res=await axios.get('http://interview-api-t.itheima.net/h5/interview/show',{
          params:{
            id:this.$route.params.id
          },
          headers:{
            Authorization:`Bearer ${this.token}`
          }
        })
        // console.log(res)
        this.obj=res.data.data
        
      },
      async handleLike(){
        //http://interview-api-t.itheima.net/h5/interview/opt
        //post  收藏、点赞题目/取消收藏、点赞题目接口
        console.log(this.token)
        // const res=await axios.post(`http://interview-api-t.itheima.net/h5/interview/opt`,{
        //   params:{
        //     id:this.$route.params.id,
        //     optType:1
        //   },
        //   headers:{
        //     Authorization: `Bearer ${this.token}` 
        //   }})
        //   console.log('后的kkkk',this.token)
        //   console.log(res)
          
        const res = await axios.post(`http://interview-api-t.itheima.net/h5/interview/opt`,   //post请求 ('',{data数据 },{config请求头数据 })
            { // 这是请求体
                id: this.$route.params.id,
                optType: 1,
            },
            { // 这是配置对象
                headers: {
                    Authorization: `Bearer ${this.token}`
                }
            }
        );
       console.log(res);
      },
      async handleCollect(){
        const res=await axios.post(`http://interview-api-t.itheima.net/h5/interview/opt`,{
            id:this.$route.params.id,
            optType:2,
          },
          {
            headers:{
            Authorization:`Bearer ${this.token}`}
          })
          console.log(res)
      },
      f1(){
        if(!this.f1active){
          document.querySelector('.f1').style.backgroundColor='orange'
          document.querySelector('.f1').style.color='white'
          this.handleLike()
          this.$toast('点赞成功')
        }
        else{
          document.querySelector('.f1').style.backgroundColor='white'
          document.querySelector('.f1').style.color='black'
          this.handleLike()
          this.$toast('已取消点赞')
        }
        this.f1active=!this.f1active
      },
      f2(){
        if(!this.f2active){
          document.querySelector('.f2').style.backgroundColor='orange'
          document.querySelector('.f2').style.color='white'
          this.handleCollect()
          this.$toast('已收藏')
        }
        else{
          document.querySelector('.f2').style.backgroundColor='white'
          document.querySelector('.f2').style.color='black'
          this.handleCollect()
          this.$toast('已取消收藏')
        }
        this.f2active=!this.f2active
      }
    }
}
</script>

<style scoped lang="less">
    .detail{
      .header{
        border-bottom: 1px solid #999;
        // position: fixed;
        // top: 0;
        // left: 0;
      }
      .detail-content{
        width: 345px;
        // background: orange;
        margin: auto;
        h1{
        font-size: 24px;
        // 最多超出两行省略
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .front{
        font-size: 12px;
        color: #999;
      }
      .author{
        display: flex;
        img{
         margin: 11px 6px ;
       }
       span{
        margin-left: 11px;
        line-height: 60px;
        height: 40px;
        font-size: 12px;
     
       }
      }
      
      }

      .function{
        position: fixed;
        right: 20px;
        bottom: 100px;
        display: flex;
        div{
          width: 40px;
          height: 40px;
          background-color: white;
          border-radius: 20px;
          border: 1px solid white;
          margin-right: 11px;
          text-align: center;
          line-height: 40px; 
          // box-shadow: 53333vw 53333vw 2.66667vw 1vw #ccc;;
        }
        
      }
      
    }

</style>